
package testObserver;

import java.util.ArrayList;

public class Data {
    private int data;
    private ArrayList<Weather> listeners = new ArrayList<Weather>();
    private Weather change;

    public void setData(Weather w, int d) {
        data = d;
        change = w;
        notifyChange();
    }

    public int getData() {
        return data;
    }

    private void notifyChange() {
        synchronized (listeners) {
            for (Weather w : listeners) {
                if (w != change) {
                    w.onChange();
                }
            }
        }
    }

    public void registerListener(Weather w) {
        synchronized (listeners) {
            listeners.add(w);
        }
    }

    public void unRegisterListener(Weather w) {
        synchronized (listeners) {
            listeners.remove(w);
        }
    }
}
